package com.andatsoft.laisim.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.service.PlayerService;
import com.andatsoft.laisim.utils.Common;

public class LockScreenSettingActivity extends BaseActivity {

	private LinearLayout mLayoutSlideToUnlock;
	private LinearLayout mLayoutShowDateTime;
	private LinearLayout mLayoutShowAlbumArt;
	private LinearLayout mLayoutShowSeekBar;

	private CheckBox mChkBoxUseLaisimLock;
	private CheckBox mChkBoxSlideToLock;
	private CheckBox mChkBoxShowDateTime;
	private CheckBox mChkBoxShowAlbumArt;
	private CheckBox mChkBoxShowSeekBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_lock_screen);
		initViews();
		setupListeners();
		restoreSettings();
	}

	private void initViews() {
		mLayoutSlideToUnlock = (LinearLayout) findViewById(R.id.layout_setting_lock_screen_slide_to_unlock);
		mLayoutShowDateTime = (LinearLayout) findViewById(R.id.layout_setting_lock_screen_show_date_time);
		mLayoutShowAlbumArt = (LinearLayout) findViewById(R.id.layout_setting_lock_screen_show_album_art);
		mLayoutShowSeekBar = (LinearLayout) findViewById(R.id.layout_setting_lock_screen_show_seek_bar);

		mChkBoxUseLaisimLock = (CheckBox) findViewById(R.id.chk_box_lock_screen_use_laisim_lock_screen);
		mChkBoxSlideToLock = (CheckBox) findViewById(R.id.chk_box_lock_screen_slide_to_unlock);
		mChkBoxShowDateTime = (CheckBox) findViewById(R.id.chk_box_lock_screen_show_date_time);
		mChkBoxShowAlbumArt = (CheckBox) findViewById(R.id.chk_box_lock_screen_show_album_art);
		mChkBoxShowSeekBar = (CheckBox) findViewById(R.id.chk_box_lock_screen_show_seek_bar);
	}

	private void setupListeners() {

		mChkBoxUseLaisimLock.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				doWhenUseLaisimLockChanged(mChkBoxUseLaisimLock.isChecked());
				// save setting
				SharedPreferences sp = getSharedPreferences(
						Common.SP_LOCK_SCREEN_SETTING_NAME,
						Context.MODE_PRIVATE);
				SharedPreferences.Editor ed = sp.edit();
				ed.putBoolean(Common.SP_LOCK_SCREEN_SETTING_USE_LAISIM_LOCK,
						mChkBoxUseLaisimLock.isChecked());
				ed.commit();

				// send broadcast
				sendBroadcast(new Intent(
						PlayerService.INTENT_LOCK_SCREEN_SETTING_CHANGED));

			}
		});

		mChkBoxSlideToLock.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting
				SharedPreferences sp = getSharedPreferences(
						Common.SP_LOCK_SCREEN_SETTING_NAME,
						Context.MODE_PRIVATE);
				SharedPreferences.Editor ed = sp.edit();
				ed.putBoolean(Common.SP_LOCK_SCREEN_SETTING_SLIDE_TO_UNLOCK,
						mChkBoxSlideToLock.isChecked());
				ed.commit();
			}
		});

		mChkBoxShowDateTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting
				SharedPreferences sp = getSharedPreferences(
						Common.SP_LOCK_SCREEN_SETTING_NAME,
						Context.MODE_PRIVATE);
				SharedPreferences.Editor ed = sp.edit();
				ed.putBoolean(Common.SP_LOCK_SCREEN_SETTING_SHOW_DATE_TIME,
						mChkBoxShowDateTime.isChecked());
				ed.commit();
			}
		});

		mChkBoxShowAlbumArt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting
				SharedPreferences sp = getSharedPreferences(
						Common.SP_LOCK_SCREEN_SETTING_NAME,
						Context.MODE_PRIVATE);
				SharedPreferences.Editor ed = sp.edit();
				ed.putBoolean(Common.SP_LOCK_SCREEN_SETTING_SHOW_ALBUM_ART,
						mChkBoxShowAlbumArt.isChecked());
				ed.commit();
			}
		});

		mChkBoxShowSeekBar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting
				SharedPreferences sp = getSharedPreferences(
						Common.SP_LOCK_SCREEN_SETTING_NAME,
						Context.MODE_PRIVATE);
				SharedPreferences.Editor ed = sp.edit();
				ed.putBoolean(Common.SP_LOCK_SCREEN_SETTING_SHOW_SEEK_BAR,
						mChkBoxShowSeekBar.isChecked());
				ed.commit();
			}
		});
	}

	private void restoreSettings() {
		// save setting
		SharedPreferences sp = getSharedPreferences(
				Common.SP_LOCK_SCREEN_SETTING_NAME, Context.MODE_PRIVATE);
		mChkBoxUseLaisimLock.setChecked(sp.getBoolean(
				Common.SP_LOCK_SCREEN_SETTING_USE_LAISIM_LOCK, true));
		mChkBoxSlideToLock.setChecked(sp.getBoolean(
				Common.SP_LOCK_SCREEN_SETTING_SLIDE_TO_UNLOCK, true));
		mChkBoxShowDateTime.setChecked(sp.getBoolean(
				Common.SP_LOCK_SCREEN_SETTING_SHOW_DATE_TIME, true));
		mChkBoxShowAlbumArt.setChecked(sp.getBoolean(
				Common.SP_LOCK_SCREEN_SETTING_SHOW_ALBUM_ART, false));
		mChkBoxShowSeekBar.setChecked(sp.getBoolean(
				Common.SP_LOCK_SCREEN_SETTING_SHOW_SEEK_BAR, true));
		doWhenUseLaisimLockChanged(mChkBoxUseLaisimLock.isChecked());

	}

	private void doWhenUseLaisimLockChanged(boolean isChecked) {
		if (isChecked) {
			setAlpha(mLayoutSlideToUnlock, 1.0f);
			setAlpha(mLayoutShowDateTime, 1.0f);
			setAlpha(mLayoutShowAlbumArt, 1.0f);
			setAlpha(mLayoutShowSeekBar, 1.0f);
		} else {
			setAlpha(mLayoutSlideToUnlock, 0.5f);
			setAlpha(mLayoutShowDateTime, 0.5f);
			setAlpha(mLayoutShowAlbumArt, 0.5f);
			setAlpha(mLayoutShowSeekBar, 0.5f);
		}

		mChkBoxSlideToLock.setEnabled(isChecked);
		mChkBoxShowDateTime.setEnabled(isChecked);
		mChkBoxShowAlbumArt.setEnabled(isChecked);
		mChkBoxShowSeekBar.setEnabled(isChecked);

	}
}
